package appfuse.framework.database;

import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;


/**
 * 数据操作类
 * <p>Title: 数据操作类</p>
 * <p>Description: 对JDBC进行封装，用于数据操作的类</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company:</p>
 * @author savage
 * @version 1.0
 */
public class SqlCommandProc {
	private Connection conn = null;

	//private Statement stmt = null;

	private CallableStatement callstmt = null;

	/**
	 * 初始化
	 */
	public SqlCommandProc() {
		try {
			getDataSource();
			//stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	/*
	 * @Functionname:SQLCommand @Describe: 初始化PreparedStatement ��ʼ����ݿ�
	 * @param:sql sql��� @return:
	 */
	public SqlCommandProc(String sql) {
		try {
			getDataSource();
			//stmt = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE);
			callableStatement(sql);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	public void setSqlCommand(String sql){
		try {
			callableStatement(sql);
		} catch (Exception e) {
			e.getStackTrace();
		}
	}

	/**
	 * 得到数据源
	 */
	private void getDataSource() throws SqlExecException {
		//数据源采用硬编码方式 
		/*try { 
			//加载驱动程序，下面的代码为加载MySQL驱动程序
		  Class.forName("com.mysql.jdbc.Driver"); 
		  //注册MySQL驱动程序
		  DriverManager.registerDriver(new com.mysql.jdbc.Driver());
		  //用适当的驱动程序连接到数据库 
		  String dbUrl = "jdbc:mysql://localhost:3306/yegoal?useUnicode=true&characterEncoding=UTF-8";

		  String dbUser="root"; 
		  String dbPwd="12345678"; 
		  //建立数据库连接 
		  conn = java.sql.DriverManager.getConnection(dbUrl,dbUser,dbPwd);

		  //非自动提交
		  if (conn.getAutoCommit()) {
		  	conn.setAutoCommit(false);
		  }
		} catch (Exception e) { 
			e.printStackTrace(); 
			throw new SqlExecException("getDataSourceTest() error: " + e.getStackTrace()); 
		}*/

		//数据源采用JNDI
		/*try {
			Context ctx = new InitialContext();
			if (ctx == null) {
				throw new Exception("Boom - No Context");
			}

			DataSource ds = (DataSource) ctx
					.lookup("java:comp/env/jdbc/appfuse");
			if (ds != null) {
				conn = ds.getConnection();
			}

			//非自动提交
			if (conn.getAutoCommit()) {
				conn.setAutoCommit(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SqlExecException("getDataSource() error: "
					+ e.getStackTrace());
		}*/

		//数据源采用Proxool
		try {
			Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");
			conn = DriverManager.getConnection("proxool.game");

			//非自动提交
			if (conn.getAutoCommit()) {
				conn.setAutoCommit(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new SqlExecException("getDataSource() error: "
					+ e.getStackTrace());
		}
	}

	public Connection getConnection() {
		return conn;
	}

	public void callableStatement(String sql) throws SQLException {
		callstmt = conn.prepareCall("{"+sql+"}");
	}

	public void setString(int index, String value) throws SQLException {
		callstmt.setString(index, value);
	}

	public void setInt(int index, int value) throws SQLException {
		callstmt.setInt(index, value);
	}

	public void setBoolean(int index, boolean value) throws SQLException {
		callstmt.setBoolean(index, value);
	}

	public void setDate(int index, Date value) throws SQLException {
		callstmt.setDate(index, value);
	}

	public void setTime(int index, Time value) throws SQLException {
		callstmt.setTime(index, value);
	}

	public void setTimestamp(int index, Timestamp value) throws SQLException {
		callstmt.setTimestamp(index, value);
	}

	public void setLong(int index, long value) throws SQLException {
		callstmt.setLong(index, value);
	}

	public void setFloat(int index, float value) throws SQLException {
		callstmt.setFloat(index, value);
	}
	
	public void setDouble(int index, double value) throws SQLException {
		callstmt.setDouble(index, value);
	}


	public void setBinaryStream(int index, InputStream in, int length)
	throws SQLException {
		callstmt.setBinaryStream(index, in, length);
	}

	public void clearParameters() throws SQLException {
		callstmt.clearParameters();
	}

	public CallableStatement getCallableStatement() {
		return callstmt;
	}

	/**
	 * 执行
	 * 
	 * @return
	 * @throws SQLException
	 */
	public boolean execute() throws SQLException {
		if (callstmt != null) {
			return callstmt.execute();
		} else {
			return false;
		}
	}
	
	public ResultSet executeQuery() throws SQLException {
		if (callstmt != null) {
			return callstmt.executeQuery();
		} else {
			return null;
		}
	}


	/**
	 * 提交
	 */
	public void commit() throws SQLException {
		conn.commit();
	}

	/**
	 * 回滚
	 */
	public void rollback() throws SQLException {
		conn.rollback();
	}

	/**
	 * 关闭
	 */
	public void close() {
		try {
			if (callstmt != null) {
				callstmt.close();
				callstmt = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (Exception e) {
			e.getStackTrace();
		}
	}
}
